package com.sxl.listener;
import com.sxl.domain.model.IDynamicThreadPoolService;
import com.sxl.domain.model.entity.ThreadPoolConfigEntity;
import com.sxl.registry.IRegistry;
import org.redisson.api.listener.MessageListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
/**
 * @author : sxl
 * @date : 2024/8/13 15:28
 * @description: 动态线程池配置调整监听器
 */
public class ThreadPoolConfigAdjustListener implements MessageListener<ThreadPoolConfigEntity> {
    private Logger logger = LoggerFactory.getLogger(ThreadPoolConfigAdjustListener.class);

    private final IDynamicThreadPoolService dynamicThreadPoolService;

    private final IRegistry registry;

    public ThreadPoolConfigAdjustListener(IDynamicThreadPoolService dynamicThreadPoolService, IRegistry registry) {
        this.dynamicThreadPoolService = dynamicThreadPoolService;
        this.registry = registry;
    }

    //监听订阅
    @Override
    public void onMessage(CharSequence charSequence, ThreadPoolConfigEntity threadPoolConfigEntity) {
        logger.info("动态线程池，调整线程池配置。线程池名称:{} 核心线程数:{} 最大线程数:{}", threadPoolConfigEntity.getThreadPoolName(), threadPoolConfigEntity.getPoolSize(), threadPoolConfigEntity.getMaximumPoolSize());
        dynamicThreadPoolService.updateThreadPoolConfig(threadPoolConfigEntity);
        //更新后上报最新数据
        List<ThreadPoolConfigEntity> threadPoolConfigEntities = dynamicThreadPoolService.queryThreadPoolList();
        registry.reportThreadPool(threadPoolConfigEntities);
        ThreadPoolConfigEntity threadPoolConfigEntity1 = dynamicThreadPoolService.queryThreadPoolConfigByNane(threadPoolConfigEntity.getThreadPoolName());
        registry.reportThreadPoolConfigParameter(threadPoolConfigEntity1);
        logger.info("动态线程池，调整线程池配置成功。线程池名称:{} 核心线程数:{} 最大线程数:{}", threadPoolConfigEntity1.getThreadPoolName(), threadPoolConfigEntity1.getPoolSize(), threadPoolConfigEntity1.getMaximumPoolSize());
    }
}
